package com.ft.adbix.utils.download.receiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.download.DownloadUtils;

public class DownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //获取活动
        String action = intent.getAction();
        //下载完成触发广播
        if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            //获取下载编号
            //getLongExtra第二个参数为默认值
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            AdLogUtils.d("download receiver complete ="+downloadId);
//            DownloadUtils.getInstance().removeDownloadId(downloadId);
            DownloadUtils.getInstance().checkDownloadComplete(downloadId);

        }
        //点击通知(未下载完)触发通知
        else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
            AdLogUtils.d("download receiver 未完成点击通知 ");
            //获取全部下载编号
            long[] downloadIds = intent.getLongArrayExtra(DownloadManager.EXTRA_DOWNLOAD_ID);
            if (downloadIds != null && downloadIds.length >= 1) {
                //检查是否是本应用下载的
                for (long downloadId : downloadIds) {
                    //... ...
                    boolean selfTask = DownloadUtils.getInstance().checkSelfDownloadId(downloadId);

                }

            }
        }
        //点击通知(下载完)触发通知
        else if (action.equals(Intent.ACTION_VIEW)) {
            AdLogUtils.d("download receiver 下载完点击通知 ");
            //获取下载编号
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadUtils.getInstance().removeDownloadId(downloadId);
        }
    }
}
